package cn.accjiyun.gomoku;

import cn.accjiyun.gomoku.model.Chessboard;
import cn.accjiyun.gomoku.util.Logger;
import cn.accjiyun.gomoku.view.BoardLayoutController;
import cn.accjiyun.gomoku.view.RootLayoutController;
import cn.accjiyun.gomoku.view.resources.images.MyImageRes;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * Created by jiyun on 2016/9/6.
 * The main class to lunch application.
 */

public class MainApp extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;
    private RootLayoutController rootLayoutController;
    private BoardLayoutController boardLayoutController;
    private Chessboard chessboard;

    public MainApp() {
        chessboard = new Chessboard();
        chessboard.setBoardSize(15);
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public Chessboard getChessboard() {
        return chessboard;
    }

    public RootLayoutController getRootLayoutController() {
        return rootLayoutController;
    }

    public BoardLayoutController getBoardLayoutController() {
        return boardLayoutController;
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("五子棋AI");
        // Set the application icon.
        this.primaryStage.getIcons().add(MyImageRes.GOMOKUIMAGE);
        this.primaryStage.setResizable(false);
        initRootLayout();
        showBoardLayout();
    }

    /**
     * Shows the board layout inside the root layout.
     */
    public void showBoardLayout() {
        try {
            // Load person overview.
            FXMLLoader loader = new FXMLLoader(MainApp.class
                    .getResource("view/BoardLayout.fxml"));
            AnchorPane boardLayout = loader.load();

            // Set person overview into the center of root layout.
            rootLayout.setCenter(boardLayout);

            // Give the boardLayoutController access to the main app.
            BoardLayoutController.setMAINAPP(this);
            boardLayoutController = loader.getController();

            boardLayoutController.initBoard();
            boardLayoutController.setButtonFace();
        } catch (IOException e) {
            Logger.exceptionLog("Fail to load BoardLayout.fxml", e);
        }
    }

    /**
     * Initializes the root layout and tries to load the last opened
     * person file.
     */
    public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader(MainApp.class
                    .getResource("view/RootLayout.fxml"));
            rootLayout = loader.load();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);

            // Give the boardLayoutController access to the main app.
            rootLayoutController = loader.getController();
            rootLayoutController.setMainApp(this);
            rootLayoutController.adjustStageSize();

            primaryStage.show();
        } catch (IOException e) {
            Logger.exceptionLog("Fail to load RootLayout.fxml", e);
        }
    }
}
